package cn.piaoyi.web.action;

import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.web.servlet.AdminSession;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class AdminAdmin extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminAdmin.class);
	
	private AdminService adminService;

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	private long id;
	private String username;
	private String password;
	private String pwdconfirm;
	
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPwdconfirm() {
		return pwdconfirm;
	}
	public void setPwdconfirm(String pwdconfirm) {
		this.pwdconfirm = pwdconfirm;
	}
	
	private List adminList;
	
	public List getAdminList() {
		return adminList;
	}
	public void setAdminList(List adminList) {
		this.adminList = adminList;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		List l = this.getAdminService().findAdmins("id", 0);
		this.setAdminList(l);
		return SUCCESS;
	}
	
	public String add(){
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		//判断用户名和密码是否为空
		if (StringUtils.isBlank(this.username) || StringUtils.isBlank(this.password)) {
			this.addFieldError("username", ("error.nullerror"));
			return "createinput";
		}
		if(!this.password.equals(this.pwdconfirm)){
			this.addFieldError("password", this.getText("error.pwderror"));
			return "createinput";
		}
		this.username = StringUtils.trimToEmpty(this.username);
		this.password = StringUtils.trimToEmpty(this.password);
		//添加用户
		Admin a = new Admin();
		a.setUsername(this.username);
		a.setPassword(Util.hash(this.password));
		try {
			this.getAdminService().createAdmin(a);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		this.setAction("doupdate");
		Admin a = this.getAdminService().findAdminById(this.id);
		this.setUsername(a.getUsername());
		return INPUT;
	}
	
	public String doupdate(){
		if(!this.password.equals(this.pwdconfirm)){
			this.addFieldError("password", this.getText("error.pwderror"));
			return INPUT;
		}
		this.password = StringUtils.trimToEmpty(this.password);
		Admin a = this.getAdminService().findAdminById(this.id);
		a.setPassword(Util.hash(this.password));
		try {
			this.getAdminService().updateAdmin(a);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getAdminService().removeAdminById(this.id);
		return "index";
	}
}
